//index.js
//获取应用实例
const app = getApp()
const Util = require('../../utils/util.js');
Page({
  data: {
    roleAll: false,
    roleOne: true,
    roleAdd: true,
    type_id:'',
    token:'',
    cateId:''
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (e) {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          token: res.data,
          cateId: e.cate_id
        });
        that.getCateRole();
      }
    });
  },
  getCateRole: function () {
    var that = this;
    var cat_id = that.data.cateId;
    var token = that.data.token;
    Util.getCateRole({
      "cate_id": cat_id,
      "token": token
    }, function (re) {
      if (re.code == 200) {
        that.changeRole(re.data.type_id);
      }
      else {
        Util.showMsgDialog(re.msg);
      }
    }, function (err) {
      Util.showMsgDialog("操作失败");
    });
  },
  setCateRole: function() {
    var that = this;
    var cat_id = that.data.cateId;
    var token = that.data.token;
    var type_id = that.data.type_id;
    Util.setCateRole({
      "cate_id": cat_id,
      "token": token,
      "type_id":type_id
    }, function (re) {
      if (re.code == 200) {
        wx.showToast({
          title: re.msg,
          icon: 'success',
          duration: 2000
        })
      }
      else {
        Util.showMsgDialog(re.msg);
      }
    }, function (err) {
      Util.showMsgDialog("操作失败");
    });
  },
  changeRoleNumber:function(e){
    var that = this;
    var num = e.currentTarget.dataset.id;
    that.changeRole(num);
    if(num==2){
      that.checkRole();
    }
  },
  changeRole:function(num){
    var that = this;
    if(num==1){
      that.setData({
        roleAll: true,
        roleOne: false,
        roleAdd: true,
        type_id:num
      });
    }else if(num == 2){
      that.setData({
        roleAll: true,
        roleOne: true,
        roleAdd: false,
        type_id: num
      });
    }else{
      that.setData({
        roleAll: false,
        roleOne: true,
        roleAdd: true,
        type_id: num
      });
    }
  },
  checkRole:function(){
    var that = this;
    var cate_id = that.data.cateId;
    wx.navigateTo({
      url: 'role_check?cate_id='+cate_id
    })
  },
  onPullDownRefresh:function(){
    wx.startPullDownRefresh();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 300);
  }
})
